package com.ken.app.exceptions;

import com.ken.app.beans.ResultObj;
import com.ken.app.enums.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * Author: Liuk
 * Mail: leo_work@aliyun.com
 * Date: 2017/8/15 0015
 * Desc:
 */
@ControllerAdvice
public class ExceptionAdvice extends ResponseEntityExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionAdvice.class);


    /**
     * ajax 统一异常处理
     *
     * @param request
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = OAuth2Exception.class)
    @ResponseBody
    public ResultObj oauthHandler(HttpServletRequest request, OAuth2Exception e) throws Exception {
        System.out.println("监听异常信息。。。。。。。。。。。。。。。。。。");
        ResultObj resultObj = new ResultObj(ResultCode.access_token_is_missing,"token失效");
        LOGGER.error("系统异常.....",e);
        e.printStackTrace();
        return resultObj;
    }

}
